// pages/fillinfo/fillinfo.js
Page({
  data: {
    btnTxt: "提交",
    photo: ["../../images/banner1.jpg", "../../images/banner2.jpg"],
    isAddimg:true,
    imgs: [
      // { imgSrc: "../../images/banner1.jpg", isShow: true }
    ],
    photonum: 0 //当前显示的图片中最后一张图片的索引值

  },
  onLoad: function (options) {
    wx.request({
      url: 'https://jgj.hzlingdian.com/busiAdvert/find/all', //仅为示例，并非真实的接口地址
      data: {
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        console.log(res.data)
      }
    });
  },
  // 上传图片
  uploadFile() {
    var that = this;
    wx.chooseImage({
      count: 2, // 默认9
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        var tempFilePaths = res.tempFilePaths;
        if ((that.data.photonum + tempFilePaths.length)>2){
            //选择超过数量
            return;
        }
        console.log(res.tempFilePaths)
        for (var i = 0; i < tempFilePaths.length; i++){
          let imgSrc = "imgs[" + that.data.photonum+"].imgSrc";
          let isShow = "imgs[" + that.data.photonum + "].isShow";
          that.setData({ [imgSrc]: tempFilePaths[i], [isShow]:true });
          that.data.photonum++;
          that.setData({ "photonum": that.data.photonum});
          console.log(":"+that.data.photonum);
          wx.uploadFile({
            url: 'https://jgj.hzlingdian.com/coreAttachment/upload', //仅为示例，非真实的接口地址
            filePath: tempFilePaths[i],
            name: 'file',
            formData: {
              'cratmType': '997',
              'cratmDir': '997'
            },
            success: function (res) {
              var data = res.data
              console.log(data);
            }
          });
        }
        if (that.data.imgs.length==2){
          that.setData({ "isAddimg":false});
        }else{
          that.setData({ "isAddimg": true });
        }
      }
    })
  },
  // 删除上传图片
  deleteImg(e){
    var idx=e.currentTarget.dataset.idx;
    let imgSrc = "imgs[" + idx + "].imgSrc";
    let isShow = "imgs[" + idx + "].isShow";
    this.setData({ [imgSrc]:"", [isShow]: false });
    this.data.imgs.splice(idx,1);
    this.data.photonum--;
    if (this.data.imgs.length == 2) {
      this.setData({ "isAddimg": false });
    } else {
      this.setData({ "isAddimg": true });
    }
    console.log(this.data.imgs)
  },
  submitData() {
    var that = this;
    that.setData({ "btnTxt": "正在提交中..." })
  }
})